package org.ling.xuan.customnew.pannel;

import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBTextField;
import org.ling.xuan.entity.mo.CommonSetting;
import org.ling.xuan.entity.mo.LocalAdd;

import javax.swing.*;
import java.awt.*;
import java.util.Optional;

public class UpdXmlPanel extends JBPanel {
    String[] types = new String[]{"req", "sp", "base"};
    private final CommonSetting commonSetting;
    private final LocalAdd localAdd;
    public JTextField fileNameField;
    public ComboBox<String> type;
    public JTextField versionField;
    public JTextField modifyNoField;
    public JTextField requireIdField;
    public JTextField authorField;
    public JTextField customerCodeField;
    public JTextField prdType;
    public JTextField modifyMsg;
    public JTextField suffixField;
    public String fileType;
    public String nameSuffix;
    public UpdXmlPanel(CommonSetting commonSetting, LocalAdd localAdd,String fileType,String nameSuffix) {
        this.fileType =fileType;
        this.nameSuffix = nameSuffix;
        this.commonSetting = commonSetting;
        this.localAdd = localAdd;
        initPanel();
    }

    private void initPanel() {
        setLayout(new GridLayout(10, 2));
        add(new JBLabel("FileName:"));
        add(fileNameField = new JBTextField());
        add(new JBLabel("DeployType:"));
        type = new ComboBox<>(types);
        type.setSelectedIndex(0);
        add(type);
        add(new JBLabel("Version:"));
        add(versionField = new JBTextField());
        add(new JBLabel("Customer:"));
        add(customerCodeField = new JBTextField());
        add(new JBLabel("ModifyNo:"));
        add(modifyNoField = new JBTextField());
        add(new JBLabel("RequireId:"));
        add(requireIdField = new JBTextField());
        add(new JBLabel("Auth:"));
        add(authorField = new JBTextField());
        add(new JBLabel("PrdType:"));
        add(prdType = new JBTextField());
        add(new JBLabel("Msg:"));
        add(modifyMsg = new JBTextField());
        add(new JBLabel("FileType:"));
        add(suffixField = new JBTextField());
        suffixField.setText(fileType);
        if (commonSetting != null) {
            authorField.setText(commonSetting.getAuth());
        }
        type.addActionListener(e -> fileNameField.setText(getGenFileName()));
        if (localAdd != null) {
            modifyNoField.setText(localAdd.getModifyId());
            versionField.setText(localAdd.getModifyVersion());
            customerCodeField.setText(localAdd.getBankCode());
            modifyMsg.setText(localAdd.getRequirementDes());
            requireIdField.setText(localAdd.getRequirementId());
            type.setSelectedItem(localAdd.getDeployType());
        }else{
            type.setSelectedIndex(0);
        }
   

    }

    private String getGenFileName() {
        String selected = (String) type.getSelectedItem();
        if ("req".equals(selected)) {
            if (!StrUtil.isBlank(requireIdField.getText())) {
                return Optional.of(requireIdField.getText()).orElse("") + nameSuffix;
            }
        } else if ("sp".equals(selected)) {
            if (!StrUtil.isBlank(versionField.getText())) {
                String type = Optional.of(prdType.getText()).orElse("");
                if(StrUtil.isNotBlank(type)){
                    type = "-"+type;
                }
               return Optional.of(versionField.getText()).orElse("") + "-BANK-" + Optional.of(customerCodeField.getText()).orElse("") +type+ nameSuffix;
            }
        }
        return "";
    }
}
